/*---------------------------- UFSET------------------------------------------
ufset is an AU program to convert conventional 2D NMR parameters to UF 2D NMR parameters

The script needs:
					* copy ufset to TOPSPIN/exp/stan/nmr/au/src/user/
					* then compile the script
execution by topspin command: ufset
Version: 2017-07-24

Author: Laetitia ROUGER, CEISAM Laboratory, Nantes (France)
mail: laetitia.rouger@univ-nantes.fr
Supervisor: Patrick GIRAUDEAU, CEISAM Laboratory, Nantes (France)
mail: patrick.giraudeau@univ-nantes.fr
***********************************************************************/

#define MAX_SIZE 1000
char topspinversion;
char chain[BUFSIZ/2], pulprog[100], curversion[80], chirpname[BUFSIZ/2];
char pathexpno[PATH_MAX], pathfile[PATH_MAX], path_file[PATH_MAX], pathfileUFsetting[PATH_MAX], pathfileUFfeatures[PATH_MAX]; 
int digtyp, avance = 0, parmode, alreadyrun = 0, restoration = 0;
int SI1=0, SI2=0;
float LB=-170.0;
int oTD1, oTD2, oDS, oNS, TD;					// o for "old" or initial parameters
float oSWH1, oSWH2, oDW, oD1;			// o for "old" or initial parameters
float D6 = 20.0;		// in us	!! has to be a float: xx.x format required
int L4, TD1, TD2, L3, NS, nb_error =0	;
float Ta, DW, newDW, DWtest, DWout, DWmin, SWH1, error_SW1;
float Te, BW, Ge, Ga, D1, FW, tot_aq, aq;
float e1H = 0, maxGradDisp1H = 0, chirppower1H = 0;
float e13C = 0, maxGradDisp13C = 0, chirppower13C = 0, f13C = 0, decouplingpower=120.0, p3=0.0;
float p1=0.0,	hardpulsepower = 120, spinlockpower = 120;
FILE *UFsettingFile = NULL; 
FILE *UFfeaturesFile = NULL;
time_t dnow;
struct tm tm_now;
char dateofapplication[sizeof "JJ/MM/AAAA HH:MM:SS"];
char *quitmsg=NULL;
char *finalmsg=malloc((BUFSIZ/2)*sizeof(char));
int i1000 = 0;
double SF;




int copy_file(char const * const source, char const * const destination) 
{ 
    FILE* fSrc; 
    FILE* fDest; 
    char buffer[512]; 
    int NbLus; 
  
    if ((fSrc = fopen(source, "rb")) == NULL) { return 1; } 
  
    if ((fDest = fopen(destination, "wb")) == NULL) 
    { 
        fclose(fSrc); 
        return 2; 
    } 
  
    while ((NbLus = fread(buffer, 1, 512, fSrc)) != 0) 
        fwrite(buffer, 1, NbLus, fDest); 
  
    fclose(fDest); 
    fclose(fSrc); 
  
    return 0; 
}

char* ufset(const char* curdat)
{
	/* ---------- Topspin version and paths ---------- */
	getxwinvers(curversion);
	topspinversion = curversion[7];

	GETCURDATA

	if (topspinversion=='1') {
		sprintf(finalmsg, "UFSET is unavailable for Topspin 1.x versions");
		return finalmsg;
	}
	if (topspinversion=='2') { sprintf(pathexpno, "%s/data/%s/nmr/%s/%i", disk, user, name, expno); }
	if (topspinversion=='3') { sprintf(pathexpno, "%s/%s/%i", disk, name, expno); }

	/* ---------- Check pulse program ---------- */
	
	FETCHPAR("PULPROG", &pulprog)
	if ( strcmp(pulprog,"ufcosy")!=0 && strcmp(pulprog,"ufdqs")!=0 && strcmp(pulprog,"uftocsy")!=0 && strcmp(pulprog,"ufhsqc")!=0 && strcmp(pulprog,"echograd")!=0 ) {
		sprintf(finalmsg, "Current pulse program is not supported by UFSET.");
		return finalmsg;
	}

	/* ---------- Check if acquisition parameters are 2D NMR ---------- */
	FETCHPAR( "PARMODE",&parmode );
	if ( parmode !=  1 && strcmp(pulprog,"echograd")!=0) { 
		sprintf(finalmsg, "not a 2D dataset");
		return finalmsg;
	}

	/* ---------- Determine digitizer type ---------- */
	FETCHPAR("DIGTYP",&digtyp);
	if (digtyp == 8) { avance = 1;}		// HADC+
	if (digtyp == 12) { avance = 3;}	// DRU
	
	
	/* ---------- Enter parameters for echograd experiments ---------- */	
	if ( strcmp(pulprog,"echograd")==0){
		if (avance==1) {
			XCMD("2 AQ_mod qsim");
			STOREPAR("DIGMOD",0); //analog
		}
		/* scans */
		STOREPAR("DS",0);
		STOREPAR("NS",1);
		
		/* spectral width */
		STOREPAR("SW",250.0);
	
		/* delays/durations */ 
		STOREPAR("AQ",0.01);
		STOREPAR("D 2",0.01);
		STOREPAR("D 3",0.001);
		STOREPAR("D 4",0.001);
		STOREPAR("D 5",0.0055);

		/* gradients */
		STOREPAR("GPZ 0",0.0);
		STOREPAR("GPZ 1",10.0);
		STOREPAR("GPZ 2",10.0);
		
		/* chirp pulse */
		STOREPAR("SPNAM1","UF-chirp-15-11");
		STOREPAR("P 11",15000.0);
		if (topspinversion=='2') { STOREPAR("SP 1",120.0);	}
		if (topspinversion=='3') { STOREPAR("SPW 1",0);	}
	
		/* disable sample spinning */
		STOREPAR("RO",0);
	
		/* processing parameters */
		FETCHPAR("TD",&TD);
		STOREPAR("SI",TD*2);
		STOREPAR("WDW",2);
		STOREPAR("LB",-20.0);
		STOREPAR("GB",0.5);		
		
		
		sprintf(finalmsg, "Parametrization of echograd experiment done.");
		return finalmsg;
	}
	
	/* ---------- Check arguments and if UFSET has already been run ---------- */
	sprintf(pathfileUFsetting, "%s/UFsetting.txt", pathexpno);
	UFsettingFile = fopen(pathfileUFsetting, "r"); 
	if (UFsettingFile != NULL) {
		alreadyrun = 1;
		fclose(UFsettingFile);
	} 
	if(i_argc==3){if (strcmp(i_argv[2],"r")==0){ restoration = 1; } } 

	/* STOP - UFSET already done */
	if (restoration==0 && alreadyrun==1) { 
		sprintf(finalmsg, "UFSET has already been run on this dataset.");
		return finalmsg;
	}
	/* STOP - no restoration available */ 
	if (restoration==1 && alreadyrun==0) { 
		sprintf(finalmsg, "No restoration files available.");
		return finalmsg;
	}
	/* restoration - then STOP */
	if (restoration==1 && alreadyrun==1) {
		sprintf(pathfile,"%s/acqu",pathexpno);
		sprintf(path_file,"%s/_acqu",pathexpno);
		remove(pathfile); 
		rename(path_file,pathfile);
	
		sprintf(pathfile,"%s/acqus",pathexpno);
		sprintf(path_file,"%s/_acqus",pathexpno);
		remove(pathfile); 
		rename(path_file,pathfile);
	
		sprintf(pathfile,"%s/acqu2",pathexpno);
		sprintf(path_file,"%s/_acqu2",pathexpno);
		remove(pathfile); 
		rename(path_file,pathfile);

		sprintf(pathfile,"%s/acqu2s",pathexpno);
		sprintf(path_file,"%s/_acqu2s",pathexpno);
		remove(pathfile); 
		rename(path_file,pathfile);
		
		sprintf(pathfile,"%s/pdata/1/proc",pathexpno);
		sprintf(path_file,"%s/pdata/1/_proc",pathexpno);
		remove(pathfile); 
		rename(path_file,pathfile);
		
		sprintf(pathfile,"%s/pdata/1/proc2",pathexpno);
		sprintf(path_file,"%s/pdata/1/_proc2",pathexpno);
		remove(pathfile); 
		rename(path_file,pathfile);
		
		sprintf(pathfile,"%s/pdata/1/procs",pathexpno);
		sprintf(path_file,"%s/pdata/1/_procs",pathexpno);
		remove(pathfile); 
		rename(path_file,pathfile);
		
		sprintf(pathfile,"%s/pdata/1/proc2s",pathexpno);
		sprintf(path_file,"%s/pdata/1/_proc2s",pathexpno);
		remove(pathfile); 
		rename(path_file,pathfile);
		
		remove(pathfileUFsetting); 
		sprintf(finalmsg, "Restoration of conventional parameters done.");
		return finalmsg;
	}
	/* OK - continue */
	/* ---------- Copy parameters files for restoration ---------- */
	sprintf(pathfile,"%s/acqu",pathexpno);
	sprintf(path_file,"%s/_acqu",pathexpno);
	copy_file(pathfile,path_file);
	
	sprintf(pathfile,"%s/acqus",pathexpno);
	sprintf(path_file,"%s/_acqus",pathexpno);
	copy_file(pathfile,path_file);
	
	sprintf(pathfile,"%s/acqu2",pathexpno);
	sprintf(path_file,"%s/_acqu2",pathexpno);
	copy_file(pathfile,path_file);
	
	sprintf(pathfile,"%s/acqu2s",pathexpno);
	sprintf(path_file,"%s/_acqu2s",pathexpno);
	copy_file(pathfile,path_file);

	sprintf(pathfile,"%s/pdata/1/proc",pathexpno);
	sprintf(path_file,"%s/pdata/1/_proc",pathexpno);
	copy_file(pathfile,path_file);
	
	sprintf(pathfile,"%s/pdata/1/proc2",pathexpno);
	sprintf(path_file,"%s/pdata/1/_proc2",pathexpno);
	copy_file(pathfile,path_file);
	
	sprintf(pathfile,"%s/pdata/1/procs",pathexpno);
	sprintf(path_file,"%s/pdata/1/_procs",pathexpno);
	copy_file(pathfile,path_file);
	
	sprintf(pathfile,"%s/pdata/1/proc2s",pathexpno);
	sprintf(path_file,"%s/pdata/1/_proc2s",pathexpno);
	copy_file(pathfile,path_file);
	
	

	
	/* ---------- Determine spectrometer features ---------- */
	sprintf(pathfileUFfeatures, "%s/stan/nmr/au/src/user/UFfeatures1H.txt", PathXWinNMRExp());
	UFfeaturesFile = fopen(pathfileUFfeatures, "r");
	fgets(chain, MAX_SIZE, UFfeaturesFile);
	e1H = atof(chain);
	fgets(chain, MAX_SIZE, UFfeaturesFile);
	maxGradDisp1H = atof(chain);			// in Hz
	fgets(chain, MAX_SIZE, UFfeaturesFile);
	chirppower1H = atof(chain);				// in dB
	fclose(UFfeaturesFile);
	
	if (strcmp(pulprog,"ufhsqc")==0) {
		sprintf(pathfileUFfeatures, "%s/stan/nmr/au/src/user/UFfeatures13C.txt", PathXWinNMRExp());
		UFfeaturesFile = fopen(pathfileUFfeatures, "r");
		fgets(chain, MAX_SIZE, UFfeaturesFile);
		e13C = atof(chain);
		fgets(chain, MAX_SIZE, UFfeaturesFile);
		f13C = atof(chain);			// in Hz
		fgets(chain, MAX_SIZE, UFfeaturesFile);
		chirppower13C = atof(chain);				// in dB
		fclose(UFfeaturesFile);
		maxGradDisp13C = maxGradDisp1H * 10.705 / 42.576 ;
	}
	
	/* ---------- Recover current acquisition parameters ---------- */
	FETCHPAR1("TD",&oTD1);
	FETCHPAR("TD",&oTD2);
	FETCHPAR1("SWH",&oSWH1);
	FETCHPAR("SWH",&oSWH2);
	FETCHPAR("DS",&oDS);
	FETCHPAR("DW",&oDW);
	FETCHPAR("NS",&oNS);
	FETCHPAR("D 1",&oD1);	 

	
	/* ---------- Calculate UF parameters ---------- */
	L4 = oDS;
	L3 = 128;
	TD1 = 1;
	TD2 = 131072;
	
	Ta = 1000000/(oSWH2*2);			// in us
	DW = 2*Ta*L3/TD2;								// in us
	if (avance==1) {
		XCMD("2 AQ_mod qsim");
		STOREPAR("DIGMOD",0); //analog
	}
	STOREPAR("DW",DW); 				// DW value authorized by Topspin 
	FETCHPAR("DW",&newDW);
	if (newDW<0.549) {newDW=0.55;}
	Ta = (TD2*newDW/(2*L3)) - D6;		// in us 


	SWH1 = 1000000/(2*(Ta + D6));   // in Hz
	error_SW1 = (oSWH2-SWH1)/oSWH2;

	
	while (error_SW1>0.05) {
		if (newDW>=0.575) {
			DWtest = DW/2;
			if (DWtest<0.549) {DWtest=0.55;}
			STOREPAR("DW",DWtest);
			FETCHPAR("DW",&DWout);
			DWmin = DWout;
			while (DWout!=newDW) {
				DWmin = DWout;
				DWtest = DWmin+(newDW-DWmin)/2;
				if (DWtest<0.549) {DWtest=0.55;}
				STOREPAR("DW",DWtest);
				FETCHPAR("DW",&DWout);
			}
			newDW=DWmin;	
			error_SW1 = 0;
		}
		else {
				TD1 = TD1*2;
				DWtest = 2*TD1 * L3 * 1000000 / (2 * oSWH2 * TD2);
				if (DWtest<0.549) {DWtest=0.55;}
				STOREPAR("DW",DWtest);
				FETCHPAR("DW",&newDW);
				Ta = (TD2*newDW/(2*L3)) - D6;		// in us 
				SWH1 = TD1*1000000/(2*(Ta + D6));   // in Hz
				error_SW1 = (oSWH2-SWH1)/oSWH2;
		}
	}
	
	DW = newDW;
	
	Ta = (TD2*DW/(2*L3)) - D6	; 
	if (strcmp(pulprog,"ufhsqc")==0) { 
		BW = f13C * oSWH1;								// in Hz
		Te = 5000.0;												// in us
		Ge = BW	* 100 / maxGradDisp13C; 	// in percent 
		Ga = ((oSWH1 * Te * 2 * e13C) / (Ta))* 100 / maxGradDisp13C; // in percent 
	}
	else {
		if (strcmp(pulprog,"ufdqs")==0) { BW = 5 * oSWH1/2; }	// in Hz
		else {BW = 5 * oSWH1;}	// in Hz
		Te = 15000.0;												// in us
		Ge = BW	* 100 / maxGradDisp1H; 		// in percent
		Ga = ((oSWH1 * Te * 2 * e1H) / (Ta))* 100 / maxGradDisp1H;   // in percent 
	}
	if (Ge>100){
		sprintf(chain,"2 DW %f",oDW);
		XCMD(chain);	
		sprintf(finalmsg, "Spectral width in the indirect dimension is too large for an ultrafast acquisition (cannot be encoded).");
		return finalmsg;
	}
	
	/* dealing with interleaving */
	if (Ga>80) {
		if  (Ga<160) {
			TD1 = TD1*2;
			L3 = 64;
			Ga = Ga / 2;
		}
		else if  (Ga<320) {
			TD1 = TD1*4;
			L3 = 32;
			Ga = Ga / 4;
		}
		else if  (Ga<640) {
			TD1 = TD1*8;
			L3 = 16;
			Ga = Ga / 8;
		}
		else if  (Ga<1280) {
			TD1 = TD1*16;
			L3 = 16;
			DW = DW * 2;
			Ga = Ga / 16;
		}
		else {   // if Ga too strong to be managed with interleaving
			sprintf(chain,"2 DW %f",oDW);
			XCMD(chain);	
			sprintf(finalmsg, "Spectral widths are too large for an ultrafast acquisition.");
			return finalmsg;
		}
	}
	
	if (TD1==16) {
		TD2 = TD2 / 2;
		DW = DW * 2;
	}
	if (TD1==32) {
		TD2 = TD2 / 4;
		DW = DW * 4;
	}
	if (TD1>32) {
		sprintf(chain,"2 DW %f",oDW);
		XCMD(chain);
		sprintf(finalmsg, "Spectral widths are too large for an ultrafast acquisition.");
		return finalmsg;
	}
	
	Ta = (TD2*DW/(2*L3)) - D6;
	
	/* Calculate new SW1 */
	SWH1 = TD1*1000000/(2*(Ta + D6));   // in Hz
	FETCHPAR("SFO1",&SF);
	sprintf(finalmsg, "SW1 = %.2f ppm. \n", SWH1/SF);


	/* processing parameters */
	SI1 = TD1 * L3 * 2;
	SI2 = TD2 / L3;
	LB = -170.0 * 261.6 / Ta; 


	D1 = oD1;
	if (oD1<3.0) {
		D1 = 3.0;
		sprintf(finalmsg, "%s D1 too short, set to 3 s \n", finalmsg);
	} 
	/* digital filter bandwidth */
	FW = Ga * maxGradDisp1H /100;																			// in Hz
	/* check acquisition duration */
	NS = oNS;
	tot_aq = D1 * (NS * TD1 + L4) ;
	if ( (oNS * TD1 + L4)>360 ) {	sprintf(finalmsg, "%s WARNING: Total acquisition duration (%i s) is not safe for the gradient coil !\n", finalmsg, tot_aq);	}
	sprintf(finalmsg, "%s WARNING: After an UF acquisition of 20 minutes (single or consecutive experiments), allow the gradient coil to rest for at least 10 min.\n", finalmsg);
	
	/* chirp pulse creation */
	sprintf(chirpname,"UF-chirp-%.0f-%.2f",Te/1000, BW/1000);
	if (strcmp(pulprog,"ufhsqc")==0) {sprintf(chain,"st generate SmoothedChirp 2000 %f %.0f 5.0 1 filename=%s",BW,Te,chirpname);}
	else {sprintf(chain,"st generate SmoothedChirp 5000 %f %.0f 5.0 1 filename=%s",BW,Te,chirpname);}
	XCMD(chain);
	
	
	/*  ---------- Set UF parameters ---------- */
	/* dummy scans */
	STOREPAR("DS",0);
	STOREPAR("L 4",L4);
	
	/* delays/durations */
	STOREPAR("D 1",D1);
	STOREPAR("DW",DW);
	
	/* nb of points */
	STOREPAR("L 3",L3);
	STOREPAR1("TD",TD1);
	STOREPAR("TD",TD2);
	
	/* spatial encoding */
	STOREPAR("SPNAM1",chirpname);
	STOREPAR("P 11",Te);
	if (strcmp(pulprog,"ufhsqc")==0) {
		if (topspinversion=='2') { STOREPAR("SP 1",chirppower13C);	}
		if (topspinversion=='3') { STOREPAR("SPdB 1",chirppower13C);	}
	}
	else {
		if (topspinversion=='2') { STOREPAR("SP 1",chirppower1H);	}
		if (topspinversion=='3') { STOREPAR("SPdB 1",chirppower1H); }
	}
	STOREPAR("GPZ 0",Ge); 
	STOREPAR("GPZ 1",-Ge); 

	/* acquisition */
	STOREPAR("NBL",1);
	STOREPAR("D 6",D6/1000000); 
	STOREPAR("GPZ 15",Ga); 
	STOREPAR("GPNAM15","SMSQ10.32");
	STOREPAR("GPZ 16",-Ga); 
	STOREPAR("GPNAM16","SMSQ10.32");
	if (strcmp(pulprog,"ufhsqc")==0) {
		STOREPAR("GPZ 25",-Ga/2-20.0);
		STOREPAR("GPZ 24",80.0);
		STOREPAR("P 24",Ta);
	}
	else {
		STOREPAR("P 24",Ta);
		if (strcmp(pulprog,"ufdqs")==0) {
			STOREPAR("GPZ 25",-(Ga/2)+80.0);
			STOREPAR("GPZ 24",-40.0);
		}
		else {
			STOREPAR("GPZ 25",-Ga/2);
			STOREPAR("GPZ 24",0.0); 
		}
	}
	STOREPAR("GPNAM25","SMSQ10.32");
	
	STOREPAR("GPNAM24","SMSQ10.32");
	STOREPAR("NS",NS);
	STOREPAR("FW",FW);
	STOREPAR1("FnMODE",1);   //QF
	if (avance==1) {
		XCMD("2 AQ_mod qsim");
		STOREPAR("DIGMOD",0); //analog
	}
	if (avance==3) {
		XCMD("2 AQ_mod DQD");
		STOREPAR("DIGMOD",1); //digital
	}

	/* coherence selection gradients */
	if (strcmp(pulprog,"ufhsqc")==0) {
		STOREPAR("P 23",2585.14);
		STOREPAR("P 26",650.0);
		STOREPAR("GPZ 23",80.0); 
		STOREPAR("GPZ 26",-80.0);
		STOREPAR("GPNAM23","SINE.50");
		STOREPAR("GPNAM26","SINE.50");
	}
	else {
		STOREPAR("P 23",1000.0);
		STOREPAR("P 26",1000.0);
		STOREPAR("GPNAM23","SINE.100");
		STOREPAR("GPNAM26","SINE.100");
		if (strcmp(pulprog,"ufdqs")==0) {
			STOREPAR("GPZ 23",-40.0); 
			STOREPAR("GPZ 26",80.0);
		}
		else { 
			STOREPAR("GPZ 23",80.0); 
			STOREPAR("GPZ 26",-80.0);
		}
	}
	
	/* "crushers" during spatial encoding */
	STOREPAR("P 20",1000.0);
	STOREPAR("GPZ 20",80.0);
	STOREPAR("GPNAM20","SINE.100");
	STOREPAR("P 21",1000.0);
	STOREPAR("GPZ 21",80.0);
	STOREPAR("GPNAM21","SINE.100");
	
	/* purge gradient */
	STOREPAR("P 27",5000.0);
	STOREPAR("GPZ 27",80.0); 
	STOREPAR("GPNAM27","SINE.100");

	/* disable sample spinning */
	STOREPAR("RO",0);
	
	/* pre-scan delay */
	STOREPAR("DE",6.5);
	
	/* processing parameters */
	STOREPAR("SI",SI2);
	STOREPAR1("SI",SI1);
	STOREPAR("WDW",2);
	STOREPAR("LB",LB);
	STOREPAR("GB",0.5);
	STOREPAR1("WDW",3);
	STOREPAR1("SSB",0.0);
	STOREPAR("ME_mod",0);
	STOREPAR1("ME_mod",0);
	STOREPAR("BC_mod",0);
	STOREPAR1("BC_mod",0);		
	
	/* reading pulse in DQS */
	if (strcmp(pulprog,"ufdqs")==0) {
		FETCHPAR("P 1",&p1);
		STOREPAR("P 10",(p1*2/3));
	}
	
	/* spin lock for TOCSY */
	if (strcmp(pulprog,"uftocsy")==0) {
		sprintf(chain,"mlev16");
		STOREPAR("CPDPRG1",chain);
		STOREPAR("PCPD 1",40.0);
		STOREPAR("D 28",0.08);
		if (topspinversion=='2') { FETCHPAR("PL 1",&hardpulsepower);	}
		if (topspinversion=='3') { FETCHPAR("PLdB 1",&hardpulsepower);	} 
		FETCHPAR("P 1",&p1);
		spinlockpower=hardpulsepower+20*(log10((40/p1)));
		if (topspinversion=='2') { STOREPAR("PL 12",spinlockpower);	}
		if (topspinversion=='3') { STOREPAR("PLdB 12",spinlockpower); }
	}

	/* decoupling for HSQC */
	if (strcmp(pulprog,"ufhsqc")==0) {	
		sprintf(chain,"p5m4sp180");
		STOREPAR("CPDPRG2",chain); 
		//create decoupling pulse shape
		sprintf(chain,"st generate CaWurst 250 123000.0 1024.0 2.0 1 filename=caWURST-250-123k-1024-2");
		XCMD(chain);
		sprintf(chain, "caWURST-250-123k-1024-2");
		STOREPAR("SPNAM15",chain);
		STOREPAR("PCPD 2",1024.0);
		//calculate decoupling power = 12.9 kHz (maximal amplitude)
		if (topspinversion=='2') { FETCHPAR("PL 2",&hardpulsepower);	}
		if (topspinversion=='3') { FETCHPAR("PLdB 2",&hardpulsepower);	} 
		FETCHPAR("P 3",&p3);
		decouplingpower=hardpulsepower+20*(log10(1/(4*p3/1000000))-log10(12900));
		if (topspinversion=='2') { 
			STOREPAR("PL 12",decouplingpower);	
			STOREPAR("SP 15",decouplingpower);
		}
		if (topspinversion=='3') { 
			STOREPAR("PLW 12",10.0); 
			STOREPAR("SPW 15",10.0);
		}
		STOREPAR("P 17",1000.0);
		STOREPAR("GPZ 17",10.0);
		STOREPAR("GPNAM17","SMSQ10.32");
		STOREPAR("P 30",10000.0);
		STOREPAR("GPZ 30",80.0);
		STOREPAR("GPNAM30","SMSQ10.32");
	}
	
	/* ---------- Check acquisition duration and power amplitudes ---------- */
	FETCHPAR("AQ",&aq);
	if (aq>0.3) { sprintf(finalmsg, "%s WARNING: calculated AQ is not safe for the hardware !\n", finalmsg); }

	/* ---------- Creation of the UFsetting file ---------- */
	UFsettingFile = fopen(pathfileUFsetting, "w"); 
  dnow = time(NULL);
  tm_now = *localtime(&dnow);
  strftime(dateofapplication, sizeof dateofapplication, "%d/%m/%Y %H:%M:%S", &tm_now);
  sprintf(chain,"UFSET applied on the %s",dateofapplication);
	fprintf(UFsettingFile, chain); 	
	fclose(UFsettingFile);
	
	/* ---------- End of ufset - returns the message to be displayed ---------- */
	return finalmsg;
}


quitmsg = ufset(curdat);
QUITMSG(quitmsg)